const db = require('../config/DB');

class HeroesModel {
  //查找所有
  findAll() {
    return db.query('SELECT * FROM heroes WHERE `status`=1 ;', []);
  }

  //根据id查找
  findById(id) {
    return db.query('SELECT * FROM heroes WHERE `status`=1 And `id`=? ;', [id]);
  }

  //根据name查找
  findByName(name) {
    return db.query('SELECT * FROM heroes WHERE `name`=?', [name]);
  }

  //查询数据是否存在
  findIsExist(name) {
    return db.query('SELECT * FROM heroes WHERE `name`=? and status=1', [name]);
  }

  //根据id编辑
  update(body, id) {
    return db.query("UPDATE heroes SET ? WHERE id=? ;", [body, id]);
  }

  //根据id删除
  delete(id) {
    return db.query("UPDATE heroes SET status=0 WHERE id=? ;", [id]);
  }

  //添加数据
  create(body) {
    return db.query("INSERT INTO heroes SET ? ;", [body]);
  }

  //恢复数据
  restore(body, id) {
    return db.query('UPDATE heroes SET ?,status=1 WHERE id=?', [body, id]);
  }
}

module.exports = new HeroesModel();